package com.tengju.bff.interfaces.manage.star.facade;

import com.tengju.bff.interfaces.shared.PageAssembler;
import com.tengju.bff.interfaces.manage.star.vo.ListStarDetailVO;
import com.tengju.bff.interfaces.manage.star.vo.StarDetailVO;
import com.tengju.bff.interfaces.shared.MobileVO;
import com.tengju.bff.interfaces.manage.star.vo.*;
import com.tengju.bff.interfaces.shared.MobileVO;
import com.tengju.bff.interfaces.shared.PageAssembler;
import com.tengju.user.application.archives.dto.StarListQueryDTO;
import com.tengju.user.domain.model.sign.StarContractInfo;
import com.tengju.user.domain.model.sign.StarLimitInfo;
import com.tengju.user.domain.model.sign.StarSignInfoRepository;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.model.user.UserInfoRepository;
import com.tengju.user.domain.model.user.UserStarInfo;
import com.tengju.user.domain.service.UserDocService;
import com.tengju.user.domain.shared.BasePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class StarManageFacade {

    @Autowired
    UserInfoRepository userInfoRepository;

    @Autowired
    StarSignInfoRepository starSignInfoRepository;

    @Autowired
    UserDocService userDocService;

    public ListStarDetailVO getListStarDetail(StarListQueryDTO starListQueryDTO) {
        PageAssembler.convert(starListQueryDTO);
        BasePage<UserInfo> userInfoBasePage = userDocService.listPageStarUser(
                Optional.ofNullable(starListQueryDTO.getIdCode())
                        .map(UserIdCode::new)
                        .orElse(null),
                starListQueryDTO.getMobile(),
                starListQueryDTO.getNickName(),
                starListQueryDTO.getStarLevel(),
                starListQueryDTO.getOffset(),
                starListQueryDTO.getPageSize()
        );
        List<UserInfo> userInfoList = userInfoBasePage.getDatas();
        List<UserStarInfo> userStarInfos = starSignInfoRepository.listUserStarInfo(userInfoList);
        List<StarDetailVO> starDetailVOList = userStarInfos.stream().map(v -> {
            Long idCode = v.getIdCode().getValue();
            LocalDateTime starLevelLastChangeTime = v.getStarLevelLastChangeTime();
            UserInfo userInfo = v.getUserInfo();
            StarContractInfo starContractInfo = v.getStarContractInfo();
            StarLimitInfo starLimitInfo = v.getStarLimitInfo();
            StarDetailVO starDetailVO = new StarDetailVO();
            //TODO use user id
//            starDetailVO.setUserId(v.getUserInfo().getId());
            starDetailVO.setIdCode(idCode);
            starDetailVO.setMobile(new MobileVO(userInfo.getWholePhoneNumber()));
            starDetailVO.setNickName(userInfo.getNickName());
            starDetailVO.setStarRole(userInfo.getStarRole());
            starDetailVO.setStarLevel(userInfo.getStarLevel());
            if (starContractInfo != null) {
                starDetailVO.setAgreementStatus(starContractInfo.getContractStatus().byteValue());
                starDetailVO.setAgreementExpireDate(starContractInfo.getContractExpireDate());
            }
            starDetailVO.setUpdateTime(starLevelLastChangeTime);
            starDetailVO.setBlackListFlag(starLimitInfo != null);
            return starDetailVO;
        }).collect(Collectors.toList());
        ListStarDetailVO listStarDetailVO = new ListStarDetailVO();
        listStarDetailVO.setStarDetailVOList(starDetailVOList);
        listStarDetailVO.setTotal(userInfoBasePage.getTotal());
        listStarDetailVO.setPageIndex(starListQueryDTO.getPageIndex());
        listStarDetailVO.setPageSize(starListQueryDTO.getPageSize());
        return listStarDetailVO;
    }
}
